﻿using System;
using System.IO;
using System.Reflection;
using System.Collections.Generic;

namespace ExcelConfig
{
    using ExcelConfig.Config;
    using ExcelConfig.Template;

    public static partial class AppConfig
    {
        internal static string SheetName { get; set; }
        /// <summary>
        /// 主配置文件
        /// </summary>
        public static ExcelConfigFile ExcelConfig { get; internal set; }
        static AppConfig()
        {
            SheetName = ExcelConfigSetting.SHEET_Global;
            ExcelConfig = new ExcelConfigFile();

            ExcelConfigHelper.auto_clone_default_configfile();
        }
        public static void SetDefault(string name, string value)
        {
            ExcelConfig.SetDefault(SheetName, name, value);
        }
        public static void GotoSheet(string sheetname)
        {
            ExcelConfig.ChangeSheet(sheetname);
        }
        /// <summary>
        /// 读取全局配置值
        /// </summary>
        public static T ReadGobalValue<T>(string key)
        {
            return ExcelConfig.ReadValue<T>(SheetName, key);
        }

        public static void WriteGlobalValue(string key, string value)
        {
            ExcelConfig.WriteValue(ExcelConfigSetting.SHEET_Global, key, value);
        }
        public static void WriteValue(string key, string value)
        {
            ExcelConfig.WriteValue(SheetName, key, value);
        }
        public static string Text(string key)
        {
            return ExcelConfig.ReadText(SheetName, key);
        }
        public static decimal Number(string key)
        {
            return ReadGobalValue<decimal>(key);
        }
        public static void WriteText(string key, string value)
        {
            ExcelConfig.WriteValue(SheetName, key, value);
        }
        public static string[] Array(string key)
        {
            var arr = new Queue<string>();
            foreach (var text in LoopArray(key)) arr.Enqueue(text);
            return arr.ToArray();
        }
        public static IEnumerable<string> LoopArray(string key)
        {
            var arr = Text(key).Split(ExcelConfigSetting.ArraySplitChar);
            for (var i = 0; i < arr.Length; i++) if (!string.IsNullOrEmpty(arr[i])) yield return arr[i];
        }
        /// <summary>
        /// 读取全局开关值
        /// </summary>
        public static bool YesOrNo(string key)
        {
            return Text(key) == ExcelConfigSetting.YES;
        }
        public static void WriteYesOrNo(string key, bool value)
        {
            WriteValue(key, value?ExcelConfigSetting.YES: ExcelConfigSetting.NO);
        }
        public static void WriteYes(string key)
        {
            WriteYesOrNo(key, true);
        }
        public static void WriteNo(string key)
        {
            WriteYesOrNo(key, false);
        }
        /// <summary>
        /// 空配置文件流
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NullReferenceException"></exception>
        public static Stream EmptyStream()
        {
            // 获取当前程序集
            Assembly assembly = Assembly.GetAssembly(typeof(AppConfig));

            // 读取嵌入的资源文件
            using var cfg_sr = assembly.GetManifestResourceStream("ExcelConfig.app.config.xlsx");
            if (cfg_sr == null) throw new NullReferenceException("app.config.xlsx not exist in excelconfig.");

            var s = new ExcelTemplateStreamFile(cfg_sr);
            var mem = new MemoryStream();
            s.TemplateOut(new Dictionary<string, string> { ["ExcelConfigVersion"] = assembly.GetName().Version.ToString() }, mem);
            return mem;
        }
    }
}
